<?
	ini_set('register_globals', 		'off');
	ini_set('magic_quotes_gpc', 		'off');
	ini_set('magic_quotes_sybase', 		'off');
	ini_set('magic_quotes_runtime', 	'off');
	require("utils.php");
	require('text.class.php');	
	if(!defined('sslib_settings_path'))	define('sslib_settings_path', 'private/.settings');
	require_once ("err.class.php");
	require("request.class.php");
	
	
	Sslib::Init(sslib_settings_path);
	 
	
	date_default_timezone_set('Europe/Moscow');
	define('SSLIB_Sslib', TRUE);
	
	require("html.class.php");
	require("template.class.php");
	require('session.class.php');
	require('group.class.php');
	require('page.class.php'); //Must define Page class before session_start calling.
	
	$g_sslib_templates = array();
	
	/**
	 * Main SSLib class
	 *
	 */
	
	class Sslib
	{
	    private $siteroot = '';
	    private static $debug = true;
	    
		static function Init($settings)
		{
			$GLOBALS['sslib_settings'] = Sslib::LoadSettings($settings);
			Sslib::CompleteSettings();
			Sslib::LoadLocalizedMessages(Sslib::Variable('sslib.language'));
				
			header("Content-type: text/html; charset=".Sslib::Variable('page.charset'));
			ini_set('mbstring.internal_encoding', Sslib::Variable('page.charset'));
			
			date_default_timezone_set(Sslib::Variable('site.timezone'));
			ini_set('date.timezone', Sslib::Variable('site.timezone'));
			setlocale(LC_ALL, Sslib::Variable('site.locale'));
			include('database.class.php');
		}
		static function LoadModule($module)
		{
		    $path = Sslib::CiteRoot('modules/'.$module.'/'.$module.'.class.php');
		    if(!file_exists($path))    new Err(__T('module not found').' [ '.$module.' ]');
		    else                       include_once($path);
		}
		static function LoadLocalizedMessages($lang)
		{
			$sslibroot = Sslib::Variable('sslib.path');
			if($lang)	$file = $sslibroot.'languages/'.Sslib::Variable('sslib.language').'.inc';
			else 		include ($sslibroot.'languages/en.inc');
			
			if(file_exists($file))	include $file;
			else 					include ($sslibroot.'languages/en.inc');
		}
		private static function CompleteSettings()
		{
		    
		    $GLOBALS['sslib_settings']['site.path'] = (array_key_exists('site.path', $GLOBALS['sslib_settings'])?
				$GLOBALS['sslib_settings']['site.path']:
				str_replace("\\","/",realpath(dirname(__FILE__)."/..").'/'));
			
		    
		 	if(empty($GLOBALS['sslib_settings']['sslib.error.ignore']))
				$GLOBALS['sslib_settings']['sslib.error.ignore']='';
			if(empty($GLOBALS['sslib_settings']['log.format.date']))
				$GLOBALS['sslib_settings']['log.format.date'] = 'y-m-d h:i ';
			
		    if(empty($GLOBALS['sslib_settings']['site.path']))
		    	if(array_key_exists('SCRIPT_FILENAME',$_SERVER))
		    	{
		    		$pi = pathinfo($_SERVER['SCRIPT_FILENAME']);
		    		$GLOBALS['sslib_settings']['site.path'] = str_replace('//','/', $pi['dirname'].'/');
		    	}
		    
		    if(!is_dir($GLOBALS['sslib_settings']['site.path'])) die('missing site.path');
		    	
		    if(empty($GLOBALS['sslib_settings']['sslib.path']))
		    	$GLOBALS['sslib_settings']['sslib.path'] = $GLOBALS['sslib_settings']['site.path'].'sslib/';
		    
		    if(!is_file($GLOBALS['sslib_settings']['sslib.path'].'sslib.class.php')) die('missing sslib.path'); 
		    
		    
		    
			if(empty($GLOBALS['sslib_settings']['site.private.path']))
				$GLOBALS['sslib_settings']['site.private.path'] = Sslib::SiteRoot().'private/';

			if(empty($GLOBALS['sslib_settings']['sslib.language']))
				$GLOBALS['sslib_settings']['sslib.language'] = 'en';
			if(empty($GLOBALS['sslib_settings']['log.error.path']))
				$GLOBALS['sslib_settings']['log.error.path']='private/error.log';
			if(empty($GLOBALS['sslib_settings']['database.type']))
				$GLOBALS['sslib_settings']['database.type'] = '';
			if(empty($GLOBALS['sslib_settings']['site.static.css']))
				$GLOBALS['sslib_settings']['site.static.css'] = '';
			if(empty($GLOBALS['sslib_settings']['site.static.js']))
				$GLOBALS['sslib_settings']['site.static.js'] = '';
		}
		/**
		 * Return or create Database object. Make some checks, and reuse created Database object, if needed.
		 *
		 * @return Database
		 */
		static function DB()
		{
			
			if(!array_key_exists('sslib_database', $GLOBALS))
			{
				//if(!defined("SSLIB_Database")) die(__FILE__." Database class missing");
				$GLOBALS['sslib_database'] = new Database();				
			}
			$db = $GLOBALS['sslib_database'];
			return $db;
		}
		
		/**
		 * Return root path for the cite. If $path parameter is not null then return merged path.
		 *
		 * @param string $path
		 * @return string
		 */
		static function SiteRoot($path = null)
		{
			$sitepath = $GLOBALS['sslib_settings']['site.path'];
		    if($path != null)
				return ((strpos($path, '/') === 0)?$sitepath.substr($path, 1):$sitepath.$path);
			return $sitepath;
		}
	    static function CiteRoot($path = null){return Sslib::SiteRoot($path);}
/**
		 * Library version
		 *
		 * @return string
		 */
		public static function Version()		{return "2.0";		}
		/**
		 * Return default "users" table, where 'ownerid'->'id' column stored.
		 *
		 * @return string
		 */
		public static function TableUsers()		{return "users";	}
		public static function TableGroups()	{return "groups";	}
		public static function Debug($set = null)
		{
		    if($set === null) return Sslib::$debug;
		    
		    Sslib::$debug = $set;
		    return $set;
		}
		/**
		 * Return settings value
		 *
		 * @return string $variable
		 */
		public static function Variable($name)
		{
			if(!array_key_exists($name, $GLOBALS['sslib_settings'])) die("Variable \"$name\" not found! Exit."); 
			return $GLOBALS['sslib_settings'][$name];
		}
		
		/**
		 * Load global settings 
		 *
		 * @return void
		 */
		static function LoadSettings($path, $separator = '=')
		{
			$settings = array();
			if(!is_file($path)) die('Missing Sslib settings file path.');
			$f = fopen($path, 'r') or die('Can\'t open Sslib settings file.');
			while($val = fgets($f))
			{
				if($val[0] == '#') continue;
				$val = split($separator, $val);
				$val[0] = trim($val[0]);
				
				if(is_array($val) && !empty($val[0]))
				{
					$val[1] = str_replace('"','', trim($val[1]));
					$settings[$val[0]] = $val[1];
				}
			}
			fclose($f);
			return $settings;
		}
		static function LocalizedMessage($message)
		{
			if(array_key_exists($message, $GLOBALS['sslib_localized_messages']))
				$message = $GLOBALS['sslib_localized_messages'][$message];
			else new Err('Missing or does\'t translated message: '.$message);
			$pagecharset = Sslib::Variable('page.charset');
			if(stripos($pagecharset, 'UTF-8') === false)
			{
				$message = iconv('UTF-8', $pagecharset, $message);
			}
			return $message;
		}
		 
	}
?>